package engine.utils
{
	import engine.GameEngine;
	import engine.managers.ScreenManager;
	
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.sampler.startSampling;
	
	import org.osflash.signals.Signal;

	public class Mouse
	{
		public static var locked:Boolean = false;
		
		private static var _stage:Stage;
		private static var _isDown:Boolean = false;
		
		private static var _down:Signal;
		private static var _up:Signal;
		
		public static function construct():void
		{
			if(_stage) return;
			
			_stage = GameEngine.screenManager.stage;
			_stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			_stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
			_stage.addEventListener(MouseEvent.MOUSE_OUT, onUp);
			
			_down 	= new Signal();
			_up 	= new Signal();
		}
		
		public static function destroy():void
		{
			if(!_stage) return;
			
			_stage.removeEventListener(MouseEvent.MOUSE_DOWN, onDown);
			_stage.removeEventListener(MouseEvent.MOUSE_UP, onUp);
			_stage.removeEventListener(MouseEvent.MOUSE_OUT, onUp);
			_stage = null;
			
			_down.removeAll();
			_down = null;
			
			_up.removeAll();
			_up = null;
		}
		
		public static function isDown():Boolean { return locked ? false : _isDown; }
		
		public function get down():Signal 	{ return _down; }
		public function get up():Signal 	{ return _up; }
		
		private static function onDown(event:MouseEvent):void
		{
			_isDown = true;
			if(!locked) _down.dispatch();
		}
		
		private static function onUp(event:MouseEvent):void
		{
			_isDown = false;
			if(!locked) _up.dispatch();
		}
	}
}